Option Strict On
Option Explicit On

'Copyright (c) 2007-2010, William Severance, Jr., WESNet Designs
'All rights reserved.

'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:

'Redistributions of source code must retain the above copyright notice, this list of conditions
'and the following disclaimer.

'Redistributions in binary form must reproduce the above copyright notice, this list of conditions
'and the following disclaimer in the documentation and/or other materials provided with the distribution.

'Neither the name of William Severance, Jr. or WESNet Designs may be used to endorse or promote
'products derived from this software without specific prior written permission.

'Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'            OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'            AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
'            FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'            INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'            OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
'            IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Although I will try to answer questions regarding the installation and use of this software when
'such questions are submitted via e-mail to the below address, no promise of further
'support or enhancement is made nor should be assumed.

'Developer Contact Information:
'     William Severance, Jr.
'     WESNet Designs
'     679 Upper Ridge Road
'     Bridgton, ME 04009
'     Phone: 207-647-9375
'     E-Mail: bill@wesnetdesigns.com
'     Website: www.wesnetdesigns.com

Imports DotNetNuke.Services.Exceptions

Namespace WESNet.DNN.Modules.EPrayer
    Partial Class MyPosts
        Inherits EPrayerModuleBase

#Region "Private Members"
        Private _OrderMyPostsBy As String = "Order_Date_DESC"
        Private _LastCategory As String = String.Empty
#End Region

#Region "Public Properties"

        Public Overrides Property PageSize() As Integer
            Get
                If trPager.Visible Then
                    Return Convert.ToInt32(ddlPageSize.SelectedValue)
                Else
                    Return MyBase.PageSize
                End If
            End Get
            Set(ByVal value As Integer)
                If trPager.Visible Then
                    Dim li As ListItem
                    li = ddlPageSize.Items.FindByValue(value.ToString)
                    If Not li Is Nothing Then
                        ddlPageSize.SelectedIndex = ddlPageSize.Items.IndexOf(li)
                        MyBase.PageSize = value
                    End If
                Else
                    MyBase.PageSize = value
                End If
            End Set
        End Property

        Public Property OrderMyPostsBy() As String
            Get
                If ddlOrderMyPostsBy.SelectedIndex <> -1 Then
                    Return ddlOrderMyPostsBy.SelectedValue
                Else
                    Return _OrderMyPostsBy
                End If
            End Get
            Set(ByVal value As String)
                Dim li As ListItem = ddlOrderMyPostsBy.Items.FindByValue(value)
                If Not li Is Nothing Then
                    ddlOrderMyPostsBy.SelectedIndex = ddlOrderMyPostsBy.Items.IndexOf(li)
                    _OrderMyPostsBy = value
                End If
            End Set
        End Property


#End Region

#Region "Public and Protected Methods"

        Protected Function GetNavigationParams() As String()
            Return GetNavigationParams(False)
        End Function

        Protected Function GetNavigationParams(ByVal Print As Boolean) As String()
            Dim params As New ArrayList
            Dim result As String()
            If PageSize <> UserInfo.PageSize Then params.Add("pagesize=" & PageSize.ToString)
            If CurrentPage <> 1 Then params.Add("currentpage=" & CurrentPage.ToString)
            If OrderMyPostsBy <> UserInfo.OrderBy Then params.Add("orderby=" & OrderMyPostsBy)
            params.Add("UserId=" & UserId.ToString)
            result = CType(params.ToArray(GetType(String)), String())
            Return result
        End Function

        Protected Function GetQueryStringParams() As String
            Dim sb As New StringBuilder
            Dim params As String() = GetNavigationParams()
            For Each s As String In params
                If Not s.StartsWith("currentpage") Then
                    sb.Append("&")
                    sb.Append(s)
                End If
            Next
            Return sb.ToString
        End Function
#End Region

#Region "Private Methods"
        Private Sub GetMyPosts()
            Dim TotalRecords As Integer
            Dim epc As New EPrayerController
            Dim objMyPosts As Generic.List(Of MyPostInfo)

            objMyPosts = epc.GetMyPosts(ModuleId, UserId, OrderMyPostsBy, PageSize, CurrentPage - 1, TotalRecords)
            If objMyPosts.Count > 0 Then
                gvMyPosts.DataSource = objMyPosts
                gvMyPosts.DataBind()
                With ctlPager
                    .TotalRecords = TotalRecords
                    .QuerystringParams = "ctl=myposts&mid=" & Me.ModuleId.ToString & GetQueryStringParams()
                    .Visible = PageSize <> 0 And TotalRecords > PageSize
                End With
                trPager.Visible = True
                trError.Visible = False
            Else
                trPager.Visible = False
                trError.Visible = True
                lblError.Text = LocalizeSharedResource("No_Posts_Found")
            End If
        End Sub
#End Region

#Region "Event Handlers"

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try
                If IsAnonymous Then
                    Response.Redirect(AccessDeniedURL(Localize("Access_Denied")), True)
                End If

                If Not String.IsNullOrEmpty(Request.QueryString("currentpage")) Then
                    CurrentPage = Convert.ToInt32(Request.QueryString("currentpage"))
                Else
                    CurrentPage = 1
                End If

                Dim RequestedPageSize As Integer
                If Not String.IsNullOrEmpty(Request.QueryString("pagesize")) Then
                    RequestedPageSize = Convert.ToInt32(Request.QueryString("pagesize"))
                Else
                    RequestedPageSize = UserInfo.PageSize
                End If

                If Not Page.IsPostBack Then
                    PopulatePageSizes(ddlPageSize, RequestedPageSize)
                    PageSize = RequestedPageSize
                Else
                    If PageSize = -1 Then PageSize = RequestedPageSize
                End If

                ctlPager.PageSize = RequestedPageSize
                ctlPager.CurrentPage = CurrentPage
                ctlPager.TabID = TabId

                If Not IsPostBack Then
                    With UserInfo
                        Select Case .IdentifyBy
                            Case 0
                                lblAuthorname.Text = .Username
                            Case 1
                                lblAuthorname.Text = .DisplayName
                            Case 2
                                lblAuthorname.Text = .Alias
                        End Select

                    End With

                    If Not String.IsNullOrEmpty(Request.QueryString("orderby")) Then
                        OrderMyPostsBy = Request.QueryString("orderby")
                    Else
                        OrderMyPostsBy = UserInfo.OrderBy
                    End If
                    Utilities.LocalizeGridView(gvMyPosts, LocalResourceFile)
                    GetMyPosts()
                End If
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Protected Sub ddlOrderMyPostsBy_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrderMyPostsBy.SelectedIndexChanged
            CurrentPage = 1 'reset to first page when sort order is changed
            Response.Redirect(EditUrl("", "", "myposts", GetQueryStringParams()), True)
        End Sub

        Protected Sub ddlPageSize_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPageSize.SelectedIndexChanged
            CurrentPage = 1 'reset to first page when filter is applied
            Response.Redirect(EditUrl("", "", "myposts", GetNavigationParams()), True)
        End Sub

        Protected Sub gvMyPosts_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvMyPosts.RowDataBound
            If OrderMyPostsBy = "Order_Category" AndAlso _
                     e.Row.RowType = DataControlRowType.DataRow Then

                Dim Category As String = CType(e.Row.DataItem, MyPostInfo).Category
                If Category = _LastCategory And e.Row.RowIndex <> 0 Then
                    e.Row.Cells(2).Text = ""
                Else
                    _LastCategory = Category
                    e.Row.Cells(2).Style.Add(HtmlTextWriterStyle.FontWeight, "Bold")
                End If
            End If
        End Sub

        Protected Sub lnkReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdReturn.Click
            Try
                Response.Redirect(ReturnUrl, True)
            Catch exc As Exception
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

#End Region
    End Class
End Namespace
